package com.qs.eggs.integral.activity;

import android.content.Intent;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.account.LoginActivity;
import com.qs.eggs.api.HttpApi;
import com.qs.eggs.base.activity.BaseActivity;
import com.qs.eggs.widgets.count.SnappingStepper;
import com.qs.eggs.widgets.count.SnappingStepperValueChangeListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

public class InegralDetialActivity extends BaseActivity {
    private SnappingStepper snappingStepper;

    private String proid,proname;
    private String proIntegral;
    private TextView mIntegral,myTvScore;
    private int myScore;
    private Button toDuiHuan;
    private WebView webView;
    private String count="1";

    @Override
    protected int getContentView() {
        return R.layout.activity_inegral_detial;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        webView=(WebView)findViewById(R.id.webView);
        myTvScore=(TextView)findViewById(R.id.tv_myscore);
        mIntegral=(TextView)findViewById(R.id.tv_prointegral);
        toDuiHuan=(Button)findViewById(R.id.btn_duihuan);
        proid=getIntent().getStringExtra("proid");
        proname=getIntent().getStringExtra("proname");
        proIntegral=getIntent().getStringExtra("proIntegral");
        snappingStepper=(SnappingStepper)findViewById(R.id.step_count);
        snappingStepper.setValue(1);
        snappingStepper.setMinValue(1);
        snappingStepper.setOnValueChangeListener(new SnappingStepperValueChangeListener() {
            @Override
            public void onValueChange(View view, int value) {
                count=value+"";
            }
        });
        mIntegral.setText("积分："+proIntegral);
        toDuiHuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(myScore<Integer.parseInt(proIntegral)){
                    Toast.makeText(InegralDetialActivity.this,"积分不足",Toast.LENGTH_SHORT).show();
                }else {

                    int total=Integer.parseInt(proIntegral)*Integer.parseInt(count);
                    HashMap<String,String> map=new HashMap<String, String>();
                    map.put("proid",proid);
                    map.put("proname",proname);
                    map.put("count",count);
                    map.put("changezonescore",proIntegral);
                    map.put("totalchangezone",total+"");
                    Gson gson=new Gson();
                    String data=gson.toJson(map);
                    OkGo.post(Constants.URL_BASE+"changezoneServlet?action=insertchangeZoneOrder")
                            .params("data",data)
                            .execute(new StringCallback() {
                                @Override
                                public void onSuccess(String s, Call call, Response response) {

                                    if(s.equals("\"true\"")){
                                        Toast.makeText(InegralDetialActivity.this,"兑换成功",Toast.LENGTH_SHORT).show();
                                        startActivity(new Intent(InegralDetialActivity.this,ExchangeOrderActivity.class));
                                    }else {
                                        Toast.makeText(InegralDetialActivity.this,"兑换失败",Toast.LENGTH_SHORT).show();

                                    }
                                }
                            });

                }
            }
        });
    }

    //获取个人信息
    public void getInfo(){
        OkGo.post(Constants.URL_BASE+"personal?action=getPersonalInfo")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONArray jsonArray = new JSONArray(s);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                myScore=Integer.parseInt(jsonArray.getJSONObject(0).getString("score"));
                            }
                            myTvScore.setText("您当前积分为:"+myScore);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }


    private void getData(){
        HttpApi.cheakIsLogin(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if(s.equals("\"true\"")){
                    getInfo();
                }else {
                    LoginActivity.show(InegralDetialActivity.this);
                }
            }
        });
        HashMap<String,String> map=new HashMap<>();
        map.put("id",proid);
        Gson gson=new Gson();
        String params=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"changezoneServlet?action=getchangeZoneDetail")
                .params("params",params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        try {
                            JSONArray jsonArray=new JSONArray(s);
                            JSONObject jsonObject=jsonArray.getJSONObject(0);

                            webView.loadData(jsonObject.getString("note"),"text/html","utf-8");

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
